class FabricsController < ApplicationController
  
  before_filter :authorize
  
  layout :determine_layout
  
  
  # This is a comment about the index method of the the controller
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @fabric_pages, @fabrics = paginate :fabrics, :per_page => 10
  end

  def show
    @fabric = Fabric.find(params[:id])
  end

  def new
    can_edit
    @fabric = Fabric.new
  end

  def create
    @fabric = Fabric.new(params[:fabric])
    if @fabric.save
      flash[:notice] = 'Fabric was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    can_edit
    @fabric = Fabric.find(params[:id])
  end

  def update
    @fabric = Fabric.find(params[:id])
    if @fabric.update_attributes(params[:fabric])
      flash[:notice] = 'Fabric was successfully updated.'
      redirect_to :action => 'show', :id => @fabric
    else
      render :action => 'edit'
    end
  end

  def destroy
    Fabric.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def can_edit
    unless session[:user_is_admin]
      flash[:notice] = 'You do not have sufficient priviliges to perform this action.'
      redirect_to :action => 'list'
    end
  end
end
